package tuiLayer;
import controlLayer.*;
import modelLayer.*;
import java.util.*;

public class ItemUI
{
    private ItemCtr itemCtr;

    public ItemUI(){
        itemCtr = new ItemCtr();

    }

    public void start(){
        mainMenuItem();
    }

    private void mainMenuItem(){
        boolean stop=false;
        while(!stop){
            int choice = writeMainMenuitem();

            switch(choice){
                case 0:
                stop = true;
                break;             
                case 1:
                createItem();
                break;
                case 2:
                findItem();
                break;
                case 3:
                updateItem();
                break;
                case 4:
                deleteItem();
                break;
                case 5:
                checkStockMenu();
                break;
                default : System.out.println("Enter a valid choice");

            }
        }
    }

    private int writeMainMenuitem(){
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\n*****Item menu*****");
        System.out.println("(0) Back");
        System.out.println("(1) Create item");
        System.out.println("(2) Find item");
        System.out.println("(3) Update item");
        System.out.println("(4) Delete item ");
        System.out.println("(5) Check stock");
        System.out.println("\nType your choice");
        int choice= keyboard.nextInt();
        return choice;
    }

    private void createItem(){
        Scanner keyboard = new Scanner(System.in);
        System.out.println("---Item creation---");
        System.out.println();

        System.out.println("Enter the name of the item:");
        String name = keyboard.nextLine();

        System.out.println("Enter the type of the item:");
        String type = keyboard.nextLine();

        System.out.println("Enter the description of the item:");
        String description = keyboard.nextLine();

        System.out.println("Enter the barcode of the item:");
        int barcode = keyboard.nextInt();
        keyboard.nextLine();

        System.out.println("Enter minimum stock value of the item:");
        int minimumNo = keyboard.nextInt();
        keyboard.nextLine();

        System.out.println("Enter maximum stock value of the item:");
        int maximumNo = keyboard.nextInt();
        keyboard.nextLine();

        System.out.println("Enter the number of items in stock:");
        int noInStock = keyboard.nextInt();
        keyboard.nextLine();

        System.out.println("Enter the price of the item:");
        int price = keyboard.nextInt();
        keyboard.nextLine();

        itemCtr.createItem(barcode, minimumNo, price, maximumNo,noInStock, name, description, type );
        System.out.println("Item successfully created!");
    }

    private void findItem(){
        Scanner keyboard = new Scanner(System.in);
        System.out.println("---Item search---");
        System.out.println();
        System.out.println("Enter item barcode:");
        int barcode = keyboard.nextInt();

        Item i=  itemCtr.findItem(barcode);
        System.out.println();
        System.out.println("---------------------------------");
        System.out.println("Barcode:\t\t" + i.getBarcode());
        System.out.println("Name:\t\t\t" + i.getName());
        System.out.println("Number in stock:\t" + i.getNoInStock());
        System.out.println("Minimum stock value:\t" + i.getMinimumNo());
        System.out.println("Maximum stock value:\t" + i.getMaximumNo());
        System.out.println("Price:\t\t\t" + i.getPrice());
        System.out.println("Type:\t\t\t" + i.getType());
        System.out.println("Description:\t\t" + i.getDescription());
        System.out.println("---------------------------------");
    }

    private void updateItem(){
        Scanner keyboard = new Scanner(System.in);
        System.out.println("---Item update---");
        System.out.println();

        System.out.println("Enter the barcode of the item:");
        int barcode = keyboard.nextInt();
        keyboard.nextLine();

        System.out.println("Enter the name of the item:");
        String name = keyboard.nextLine();

        System.out.println("Enter the type of the item:");
        String type = keyboard.nextLine();

        System.out.println("Enter the description of the item:");
        String description = keyboard.nextLine();

        System.out.println("Enter minimum stock value of the item:");
        int minimumNo = keyboard.nextInt();
        keyboard.nextLine();

        System.out.println("Enter maximum stock value of the item:");
        int maximumNo = keyboard.nextInt();
        keyboard.nextLine();

        System.out.println("Enter the number of items in stock:");
        int noInStock = keyboard.nextInt();
        keyboard.nextLine();

        System.out.println("Enter the price of the item:");
        int price = keyboard.nextInt();
        keyboard.nextLine();

        itemCtr.updateItem(barcode, noInStock, name, description,type,minimumNo, maximumNo, price);
        System.out.println("Item successfully updated!");
    }

    private void deleteItem(){
        Scanner keyboard = new Scanner(System.in);
        System.out.println("---Item removal---");
        System.out.println();

        System.out.println("Enter the barcode of the item");
        int barcode = keyboard.nextInt();

        itemCtr.deleteItem(barcode);
        System.out.println("Item successfuly deleted!");
    }

    private void checkStockMenu(){
        Scanner keyboard = new Scanner(System.in);
        boolean stop = false;
        while(!stop){
            int choice = writeCheckStockMenu();

            switch(choice){
                case 0:
                stop = true;
                break;             
                case 1:
                getNoInStock();
                break;
                case 2:
                reorderStock();
                default : System.out.println("Enter a valid choice");

            }
        }
    }
    
    private int writeCheckStockMenu(){
         Scanner keyboard = new Scanner(System.in);
        System.out.println("\n*****Stock menu*****");
        System.out.println("(0) Back");
        System.out.println("(1) Number of the item in stock");
        System.out.println("(2) Reorder stock for the item");
        System.out.println("\nType your choice");
        int choice= keyboard.nextInt();
        return choice;
    }
    
    private void getNoInStock(){
        Scanner keyboard = new Scanner(System.in);
        System.out.println("---Number of the item in stock---");
        System.out.println();

        System.out.println("Enter the barcode of the item:");
        int barcode = keyboard.nextInt();

        System.out.println("There are " +itemCtr.getNoInStock(barcode)+" items"+ itemCtr.findItem(barcode).getName() +" in stock.");  
    }
    
      private void reorderStock() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("---Item: Reorder Stock---");
        
        System.out.println("Enter the barcode of the item:");
        int barcode = keyboard.nextInt();
        
        itemCtr.reorderStock(barcode);
        System.out.println("Item was reordered! ");
    }
}